package com.tanhua.manager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.manager.service.OptionService;
import com.tanhua.pojo.Option;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/option")
public class OptionController {

    @Autowired
    private OptionService optionservice;

    @PostMapping
    public Boolean insertOne(@RequestBody Option option) {
        return optionservice.insertOne(option);
    }

    @GetMapping("/{pageNum}/{pageSize}")
    public IPage<Option> findByPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {

        IPage<Option> byPage = optionservice.findByPage(pageNum, pageSize);
        return byPage;
    }

    @GetMapping("/{questionId}")
    public List<Option> findByQuestionId(@PathVariable("questionId") Integer questionId) {
        return optionservice.findByQuestionId(questionId);
    }

    @PutMapping
    public Boolean update(@RequestBody Option option) {
        return optionservice.updateById(option);
    }

    @DeleteMapping("/{optionId}")
    public Boolean delete(@PathVariable("optionId") Integer optionId) {
        return optionservice.removeById(optionId);
    }

    @DeleteMapping("/many/{num}")
    public Boolean delete(@PathVariable("num") String num) {
        return optionservice.removeByIds(Arrays.stream(num.split(",")).collect(Collectors.toList()));
    }
}

